<template>
  <div class="authorize">
    <img src="../../../assets/img/modules/psersonal/authorize_1.png" alt="">
    <div>
      <button  class="out_btn" @click="getWx" :disabled="openBtn">微信注册/登录</button>
    </div>
    <p>点击按钮表示同意 <a href="http://gkapi.oucnet.com/static/lmy/guokai.htm">《用户使用条款》</a></p>
    <!--遮罩层-->
    <drag-loding v-if="showLoding"></drag-loding>
  </div>
</template>
<script>
import dragLoding from '@/components/loding'
export default {
  name: 'authorize',
  components: {
    dragLoding
  },
  data () {
    return {
      uniacid: 0,
      isNoLogin: 1,
      openBtn: true,
      showLoding: true
    }
  },
  props: {},
  watch: {},
  methods: {
    /*
     *@author wf_Huang
     *@Time 2019/7/15 0015 19:52
     *@function  微信授权
     *****************************************/
    getWx () {
      // 保存 渠道 活动名称  活动ID
      this.$store.state.device = this.$route.query.device
      this.$store.state.gsign = this.$route.query.gsign
      this.$store.state.uniacid = this.$route.query.uniacid
      this.$store.state.personal.activityName = this.$route.query.activityName
      this.$store.state.personal.activityId = this.$route.query.activityId
      localStorage.setItem('stateData', JSON.stringify(this.$store.state))
      var pageUrl = encodeURIComponent(window.location.href)
      window.location.href = `http://wechat.eenet.com/eeapi.php?myaction=oauth&uniacid=${this.uniacid}&scope=user&backurl=${pageUrl}`
    },
    /*
     *@author wf_Huang
     *@Time 2019/7/17 0017 13:52
     *@function  微信openid 判断是否注册
     *****************************************/
    openIDLogin () {
      this.$http({
        url: `${window.SITE_CONFIG['personalURL']}/api/ajax.User/wxLogin`,
        method: 'post',
        data: {
          gsign: this.$store.state.gsign,
          openId: this.$store.state.openid,
          nickname: this.$store.state.personal.nickname,
          avatar: this.$store.state.personal.avatar
        },
        withCredentials: false
      }).then(({ data: res }) => {
        if (res.status === 203) {
          let tostTab = this.$toast('未绑定手机号，即将前往绑定页')
          let _this = this
          setTimeout(function () {
            tostTab.clear()
            _this.$router.push({ path: '/personal-login' })
          }, 2000)
          return false
        }
        if (res.status !== 200) {
          this.$toast('授权登录失败,请重试')
          return false
        }
        this.$store.state.personal.phone = res.data.phone
        this.$store.state.personal.userData = res.data
        localStorage.setItem('stateData', JSON.stringify(this.$store.state))
        if (res.data.is_student === 1) { // 学员
          this.$router.push({ path: '/personal-student' })
        } else {
          this.$router.push({ path: '/personal-member' })
        }
      }).catch(() => {})
    },
    /*
     *@author wf_Huang
     *@Time 2019/7/17 0017 13:43 北京的34
     *@function  状态判断 ?device=APP/WX/H5&gsign=gkzx&uniacid=25&activityName=ssssss&logoUrl=logo地址&activityId=23&pageH5=2
     *****************************************/
    isStatus () {
      if (this.$store.state.gsign === this.$route.query.gsign &&
        this.$store.state.uniacid === this.$route.query.uniacid &&
        this.$store.state.personal.activityName === this.$route.query.activityName &&
        this.$store.state.personal.activityId === this.$route.query.activityId &&
        this.$store.state.personal.phone) {
        // 已登录过
        this.openIDLogin()
      } else {
      }
    }
  },
  computed: {},
  created () {
  },
  mounted () {
    this.uniacid = this.$route.query.uniacid || 0
    // 添加强哥那边跳转页判断
    if (this.$route.query.backurl) {
      // 判断是否注册 ?device=APP/WX/H5&gsign=&backurl=&openid=
      this.$store.state.gsign = this.$route.query.gsign
      this.$store.state.uniacid = this.$route.query.uniacid
      localStorage.setItem('stateData', JSON.stringify(this.$store.state))
      this.$http({
        url: `${window.SITE_CONFIG['personalURL']}/api/ajax.User/wxLogin`,
        method: 'post',
        data: {
          gsign: this.$route.query.gsign,
          openId: this.$route.query.openid
        },
        withCredentials: false
      }).then(({ data: res }) => {
        if (res.status === 203) {
          let tostTab = this.$toast('未绑定手机号，即将前往绑定页')
          let _this = this
          setTimeout(function () {
            tostTab.clear()
            _this.$router.push({ path: '/personal-login', query: { openid: _this.$route.query.openid, backurl: _this.$route.query.backurl, gsign: _this.$route.query.gsign } })
          }, 2000)
          return false
        }
        if (res.status !== 200) {
          this.$toast('授权登录失败,请重试')
          return false
        }
        window.location.href = `${decodeURIComponent(this.$route.query.backurl)}&isLogin=true`
      }).catch(() => {})
    } else {
      // 判断是授权进入还是微信进入
      if (this.$route.query.openid) {
        this.$store.state.openid = this.$route.query.openid
        this.$store.state.personal.nickname = this.$route.query.nickname
        this.$store.state.personal.avatar = this.$route.query.headimgurl
        localStorage.setItem('stateData', JSON.stringify(this.$store.state))
        this.openIDLogin()
      } else {
        // 判断是否是H5进来的
        if (this.$route.query.pageH5 === '1') {
          this.getWx()
        } else {
          if (this.$store.state.gsign === this.$route.query.gsign &&
            this.$store.state.uniacid === this.$route.query.uniacid &&
            this.$store.state.personal.activityName === this.$route.query.activityName &&
            this.$store.state.personal.activityId === this.$route.query.activityId &&
            this.$store.state.personal.phone) {
            // 已登录过
            this.openIDLogin()
          } else {
            // 判断进入游客中心
            if (this.$store.state.personal.isNoLogin === 1) {
              this.$store.state.personal.isNoLogin = 2
              this.$router.push({ path: '/personal-tourist',
                query: { gsign: this.$route.query.gsign,
                  uniacid: this.$route.query.uniacid,
                  device: this.$route.query.device || 'WX',
                  logoUrl: this.$route.query.logoUrl || 'logo' } })
            } else {
              this.showLoding = false
              this.openBtn = false
            }
          }
        }
      }
    }
  },
  destroyed () {
  }
}
</script>
<style lang="scss" scoped>
  .authorize {
    padding: 150px 50px;
    text-align: center;
    img{
      width: 220px;
    }
    .out_btn{
      border: 1px solid #C7161D;
      border-radius: 10px;
      color: #ffffff;
      background-color: #C7161D;
      line-height: 88px;
      font-size: 32px;
      letter-spacing: 0;
      text-align: center;
      display: block;
      margin: 100px 0 50px;
      display: block;
      width: 100%;
    }
    p{
      color: #999999;
      font-size: 28px;
      a{
        color: #333333;
      }
    }
  }
</style>
